
library(matchMulti)
library(dplyr)

rm(list=ls())

# Load Data
data(catholic_schools)

# Covariate Lists
student.cov <- c('minority','female','ses')
school.cov <- c('minority_mean', 'female_mean', 'size', 'acad', 'discrm', 'ses_mean')
all.cov <- c('minority','female','ses','minority_mean', 'female_mean', 'size', 'acad', 'discrm', 'ses_mean')

# Remove Non-Coed Schools
catholic_schools <- catholic_schools %>% filter(female_mean>.30, female_mean<.75)


#####################################################################
##                                 Matching                        ##
#####################################################################

# Create Discrete Measures from Continuous Covariates for Fine Balance
catholic_schools$acad_cut <- cut(catholic_schools$acad, 2)
catholic_schools$size_cut <- cut(catholic_schools$size, 2)
catholic_schools$discrm_cut <- cut(catholic_schools$discrm, 2)
catholic_schools$ses_cut <- cut(catholic_schools$ses_mean, 2)
catholic_schools$fem_cut <- cut(catholic_schools$female_mean, 2)
catholic_schools$min_cut <- cut(catholic_schools$minority_mean, 2)


l1 <- c('acad_cut', 'discrm_cut')
l2 <- c('size_cut', 'ses_cut')
l3 <- c('fem_cut','min_cut')

match_1 <- matchMulti(catholic_schools, treatment = 'sector', 
                       school.id = 'school', match.students = FALSE, verbose=TRUE, 
                       student.vars = student.cov,
                       school.fb = list(l1, l2))

## Crashes R                       
match_2 <- matchMulti(catholic_schools, treatment = 'sector', 
                       school.id = 'school', match.students = FALSE, verbose=TRUE, 
                       student.vars = student.cov,
                       school.fb = list(l1, l2, l3))
                       
## No Longer Works                       
match_3 <- matchMulti(catholic_schools, treatment = 'sector', 
                       school.id = 'school', match.students = FALSE, verbose=TRUE, 
                       student.vars = student.cov, keep.target=20) 
## Crashes R                       
match_4 <- matchMulti(catholic_schools, treatment = 'sector', 
                       school.id = 'school', match.students = FALSE, verbose=TRUE, 
                       student.vars = student.cov, keep.target=20, school.fb = list(l1, l2))  